package vn.candyapp.qlct.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateTimeUtil {
	
	public static final SimpleDateFormat FORMAT_DATE = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());
	public static final SimpleDateFormat FORMAT_MONTH = new SimpleDateFormat("MM/yyyy", Locale.getDefault());
	public static final SimpleDateFormat FORMAT_YEAR = new SimpleDateFormat("yyyy", Locale.getDefault());
	
	
	public static String getNgayThangNam(Date date) {
		return getNgayThangNam(date, "dd/MM/yyyy");
	}
	
	public static String getNgayThangNam(Date date, String fmt) {
		SimpleDateFormat dfm = new SimpleDateFormat(fmt, Locale.getDefault());
		return dfm.format(date);
	}
	
	public static String getNgayThangNamHT() {
		return getNgayThangNamHT("dd/MM/yyyy");
	}
	
	public static String getNgayThangNamHT(String fmt) {
		SimpleDateFormat dfm = new SimpleDateFormat(fmt, Locale.getDefault());
		return dfm.format(Calendar.getInstance().getTime());
	}
	
	public static String getGioHT() {
		return getGioHT("HH:mm:ss");
	}
	
	public static String getGioHT(String fmt) {
		SimpleDateFormat dfm = new SimpleDateFormat(fmt, Locale.getDefault());
		return dfm.format(Calendar.getInstance().getTime());
	}
	
	public static String getGio(Date date, String fmt) {
		SimpleDateFormat dfm = new SimpleDateFormat(fmt, Locale.getDefault());
		return dfm.format(date);
	}
	
	public static String getGio(Date date) {
		return getGio(date, "HH:mm:ss");
	}
	
	public static Date getFirstDayOfWeek(Date d) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(d);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.clear(Calendar.MINUTE);
		cal.clear(Calendar.SECOND);
		cal.clear(Calendar.MILLISECOND);
		cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
		return cal.getTime();
	}
	
	public static Date getLastDayOfWeek(Date d) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(d);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.clear(Calendar.MINUTE);
		cal.clear(Calendar.SECOND);
		cal.clear(Calendar.MILLISECOND);
		cal.add(Calendar.WEEK_OF_YEAR, 1);
		cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
		cal.add(Calendar.DATE, -1);
		return cal.getTime();
	}
	
	public static Date getNextDayOfLastDayOfWeek(Date d) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(d);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.clear(Calendar.MINUTE);
		cal.clear(Calendar.SECOND);
		cal.clear(Calendar.MILLISECOND);
		cal.add(Calendar.WEEK_OF_YEAR, 1);
		cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
		return cal.getTime();
	}
	
}
